/**
 * Created by Rafael Simon Garcia on 10/11/13.
 */

package interficie;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ModificarTarea extends JPanel implements WindowListener {
	/**
	 *
	 */
	private CtrlInterficie controller;
	private static final long serialVersionUID = 1L;
    private String task;

    private JLabel TareaNameLabel(){
        JLabel label = new JLabel("Nombre de la tarea:");
        SetTitleLabelFont(label);
        label.setBounds(55, 115, 195, 15);
        return label;
    }

    private JLabel DescriptionLabel(){
        JLabel label = new JLabel("Descripcion:");
        SetTitleLabelFont(label);
        label.setBounds(55, 185, 195, 15);
        return label;
    }

    private JLabel ElementsLabel(){
        JLabel label = new JLabel("Subtareas: ");
        SetTitleLabelFont(label);
        label.setBounds(365, 115, 195, 15);
        return label;
    }

    private JLabel BackLabel(){
        JLabel label = new JLabel("Volver");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setFont(new Font("Tahoma", Font.BOLD, 15));
        label.setBounds(550, 95, 59, 14);
        return label;
    }

    private JLabel TitleLabel(){
        JLabel label = new JLabel("Realiza cambios en esta tarea");
        label.setFont(new Font("Tahoma", Font.BOLD, 15));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setBounds(0, 35, 670, 35);
        return label;
    }

    private void SetTitleLabelFont(JLabel l){ l.setFont(new Font("Tahoma", Font.BOLD, 13)); }

    private void SetInputPaneColors(JTextArea p){
        p.setBackground(new Color(51, 204, 255));
        p.setBorder(new LineBorder(new Color(102, 153, 255), 2, true));
    }

    private JTextArea TaskNameValueTextArea(String taskName){
        JTextArea ta = new JTextArea(taskName);
        SetInputPaneColors(ta);
        ta.setBounds(85, 140, 200, 25);
        ta.setOpaque(true);
        return ta;
    }

    private JTextArea DescriptionValueTextArea(String desc){
        JTextArea ta = new JTextArea(desc);
        SetInputPaneColors(ta);
        ta.setBounds(85, 210, 200, 75);
        ta.setOpaque(true);
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        return ta;
    }

    private JList ElementsJList(ArrayList<String> subTasks){
        Vector<String> listElements = new Vector<>();
        for(String n : subTasks) listElements.add(n);

        final JList l = new JList<String>(listElements);
        l.setCellRenderer(new SubTaskCellRenderer());
        l.setBorder(new LineBorder(new Color(102, 153, 255), 2, true));
        l.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {}
        });

        l.setFont(l.getFont().deriveFont(l.getFont().getSize()));
        l.setBackground(new Color(51, 204, 255));

        return l;
    }

    private JScrollPane SubTasksSrollPane(){
        JScrollPane sp = new JScrollPane();
        sp.setBounds(395, 140, 215, 145);
        sp.setViewportView(ElementsJList(controller.getListadoSubtareasTarea(task)));
        return sp;
    }

    private JButton SaveTaskButton(final JTextArea nameArea, final JTextArea descriptionArea){
        JButton button = new JButton("Guardar");
        button.setBounds(445, 402, 165, 35);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                controller.modificarTarea(task, nameArea.getText(), descriptionArea.getText(),
                        controller.getListadoSubtareasTarea(task));
                controller.Volver("mostrarTareas");
            }
        });
        return button;
    }

    private JButton EditElementsButton(){
        JButton button = new JButton("Editar Subtareas");
        button.setBounds(445, 295, 165, 25);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                controller.EditTaskElements(task);
            }
        });
        return button;
    }

    private JButton HomeButton(){
        JButton button = new JButton("");
        button.setOpaque(false);
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setIcon(new ImageIcon(CreaTarea.class.getResource("/resources/home.png")));
        button.setBounds(554, 33, 56, 61);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                controller.Volver();
            }
        });
        return button;
    }

	
	
	public ModificarTarea(CtrlInterficie c) {
		controller = c;
        InitializeWindow();
	}

    public ModificarTarea(CtrlInterficie c, String t){
        this.task = t;
        this.controller = c;
        this.InitializeWindow();
    }

    private void InitializeWindow(){
        setBackground(new Color(0, 153, 255));
        setLayout(null);
        setBounds(100, 100, 668, 531);

        add(TitleLabel());
        add(TareaNameLabel());
        add(DescriptionLabel());

        JTextArea taskNameTextArea = TaskNameValueTextArea(task);
        JTextArea descriptionTextArea = DescriptionValueTextArea(controller.getInfoTarea(task).get(1));
        add(taskNameTextArea);
        add(descriptionTextArea);



        add(ElementsLabel());

        add(SubTasksSrollPane());
        add(EditElementsButton());

        add(SaveTaskButton(taskNameTextArea, descriptionTextArea));

        add(BackLabel());
        add(HomeButton());
        
        JButton b_cancelar = new JButton("Cancelar");
        b_cancelar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.Volver("mostrarTareas");
			}
		});
        b_cancelar.setBounds(85, 408, 165, 35);
        add(b_cancelar);

        setVisible(true);
    }

	@Override
	public void windowClosed(WindowEvent arg0) { controller.activar(); }
    @Override
    public void windowActivated(WindowEvent arg0) {}
    @Override
    public void windowClosing(WindowEvent arg0){}
	@Override
	public void windowDeactivated(WindowEvent arg0) {}
	@Override
	public void windowDeiconified(WindowEvent arg0) {}
	@Override
	public void windowIconified(WindowEvent arg0) {}
	@Override
	public void windowOpened(WindowEvent arg0) {}
}
